package com.bingxue.edu.parent.controller;

import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.management.education.model.resp.StudentResp;
import com.bingxue.edu.management.education.service.ParentService;
import com.bingxue.edu.management.education.service.StudentService;
import com.bingxue.edu.parent.model.req.ParentRecommenderUpdateReq;
import com.bingxue.edu.parent.model.resp.ParentInfo;
import com.bingxue.edu.parent.util.ParentAuthUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 学员登录认证控制器
 * @author panbo
 * @since 2024/6/28
 */
@RestController
@Tag(name = "家长小程序-学员登录认证控制器")
@RequestMapping("/p/user")
@RequiredArgsConstructor
public class ParentUserCenterController {

    private final ParentService parentService;
    private final StudentService studentService;

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/info")
    public Response<ParentInfo> getUserInfo() {
        String currentParentId = ParentAuthUtil.getCurrentParentId();
        ParentInfo parent = parentService.getMapper().selectOneByIdAs(currentParentId, ParentInfo.class);
        return Response.success(parent);
    }


    @Operation(summary = "获取学生列表")
    @GetMapping("/getStudentList")
    public Response<List<StudentResp>> getStudentList() {
        List<StudentResp> studentList = studentService.getListByParentId(ParentAuthUtil.getCurrentParentId());
        return Response.success(studentList);
    }

    @Operation(summary = "设置推荐人")
    @PostMapping("/set-recommender")
    public Response<Void> setRecommender(@RequestBody @Validated ParentRecommenderUpdateReq req) {
        parentService.setRecommender(ParentAuthUtil.getCurrentParentId(), req.getRecommenderPhone());
        return Response.success();
    }

}
